package pers.rain.WebMagicDemo.PageProcessor;
import java.util.ArrayList;
import java.util.List;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import pers.rain.WebMagicDemo.Bean.*;
import pers.rain.WebMagicDemo.Dao.WebBooksDao;
import pers.rain.WebMagicDemo.Dao.impl.WebBooksDaoImpl;
public class DoubanWebPageProcessor implements PageProcessor {

	private Site site = Site.me().setRetryTimes(3).setSleepTime(100).setTimeOut(1000);
	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}

	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		int pageLength = page.getHtml().xpath("//ul[@class='subject-list']/li").nodes().size();
		System.out.println("pageLength:"+pageLength);		
		List<WebBooks> webBooksList = new ArrayList<WebBooks>();
		WebBooks webBooks = null;
		WebBooksDao webBooksDao = null;
		Html pageHtml = page.getHtml();
		try {
			int i = 1;
			for (; i <= pageLength; i++) {
				webBooks = new WebBooks();
				webBooksDao = new WebBooksDaoImpl();
				
				webBooks.setTitle(pageHtml.xpath("//ul[@class='subject-list']/li["+i+"]//div[@class='info']//a/text()").toString());
				webBooks.setIntro(pageHtml.xpath("//ul[@class='subject-list']/li["+i+"]//div[@class='info']/div[@class='pub']/text()").toString());
				webBooks.setEvaluation(Double.valueOf(pageHtml.xpath("//ul[@class='subject-list']/li["+i+"]//div[@class='info']//span[@class='rating_nums']/text()").toString()));
				webBooks.setEvaPeople(pageHtml.xpath("//ul[@class='subject-list']/li["+i+"]//div[@class='info']//span[@class='pl']/text()").toString());
				webBooks.setDescription(pageHtml.xpath("//ul[@class='subject-list']/li["+i+"]//div[@class='info']//p/text()").toString());
				webBooks.setPrice(pageHtml.xpath("//ul[@class='subject-list']/li["+i+"]//div[@class='info']//div[@class='cart-actions']//a/text()").toString());
				webBooks.setWebsite(pageHtml.xpath("//ul[@class='subject-list']/li["+i+"]//div[@class='info']/h2/a/@href").toString());
				webBooksList.add(webBooks);
				webBooksDao.insertWebBook(webBooks);
				System.out.println("webBooks["+i+"]:"+webBooks.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		//System.out.println("webBooksList:"+webBooksList.toString());
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return super.equals(obj);
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}

	public static void main(String[] args) {
		String DoubanURL="https://book.douban.com/tag/web?";
		Spider.create(new DoubanWebPageProcessor()).addUrl(DoubanURL).addPipeline(new ConsolePipeline()).thread(1).run();
	}
	
}
